package com.hzyl.famousreader.util;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;

public class AppStoreUtils {

  public static void goAppStore() {
    goAppStore(AppUtils.getAppPackageName());
  }

  /**
   * 去应用商店
   *
   * @param myAppPkg 包名
   */
  public static void goAppStore(String myAppPkg) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    switch (Build.BRAND) {
      case "samsung":
        intent.setData(Uri.parse("http://www.samsungapps.com/appquery/appDetail.as?appId=" + myAppPkg));
        intent.setPackage("com.sec.android.app.samsungapps");
        break;
      case "HUAWEI":
      case "HONOR":
        intent.setData(Uri.parse("market://details?id=" + myAppPkg));
        intent.setPackage("com.huawei.appmarket");
        break;
      case "Meizu":
        intent.setData(Uri.parse("market://details?id=" + myAppPkg));
        intent.setPackage("com.meizu.mstore");
        break;
      case "Xiaomi":
        intent.setData(Uri.parse("market://details?id=" + myAppPkg));
        intent.setPackage("com.xiaomi.market");
        break;
      case "OPPO":
        intent.setData(Uri.parse("market://details?id=" + myAppPkg));
        intent.setPackage("com.oppo.market");
        break;
      default:
        intent.setData(Uri.parse("market://details?id=" + myAppPkg));
        break;
    }

    try {
      ActivityUtils.startActivity(intent);
    } catch (Exception e) {
      try {
        Intent otherintent = new Intent(Intent.ACTION_VIEW);
        otherintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        otherintent.setData(Uri.parse("market://details?id=" + myAppPkg));
        ActivityUtils.startActivity(otherintent);
      } catch (Exception otherEx) {
        otherEx.printStackTrace();
        ToastUtils.showShort("您的手机未安装应用商城");
      }
    }
  }
}
